package org.janon.httpproxy;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public final class HttpUtil
{
	public final static int HTTP_GET = 0;
	public final static int HTTP_HEAD = 1;
	public final static int HTTP_POST = 2;
	public final static int HTTP_PUT = 3;
	public final static int HTTP_CONNECT = 4;
	public final static int HTTP_OPTIONS = 5;
	public final static int HTTP_CONNECT_CONTINUE = 6;
	public final static int HTTP_UNKNOW = -1;
	public final static String CRLF = "\r\n";
	public final static String[] METHODS = { "GET", "HEAD", "POST", "PUT", "CONNECT", "OPTIONS", "CONNECT" };
	public final static DateFormat dataFormatGMT = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.ENGLISH);
	private static ThreadGroup threadGroup = new ThreadGroup("httpproxy");
	public static String getHttpMethod(int id)
	{
		if (id < 0 || id > 6)
		{
			return null;
		}
		else
		{
			return METHODS[id];
		}
	}

	public static int getHttpMethodId(String a)
	{
		if (startsWith(a, "GET"))
		{
			return HTTP_GET;
		}
		else if (startsWith(a, "HEAD"))
		{
			return HTTP_HEAD;
		}
		else if (startsWith(a, "POST"))
		{
			return HTTP_POST;
		}
		else if (startsWith(a, "PUT"))
		{
			return HTTP_PUT;
		}
		else if (startsWith(a, "CONNECT"))
		{
			return HTTP_CONNECT;
		}
		else if (startsWith(a, "OPTIONS"))
		{
			return HTTP_OPTIONS;
		}
		else
		{
			return HTTP_UNKNOW;
		}
	}

	public static boolean startsWith(String a, String what)
	{
		return (a.toUpperCase().indexOf(what.toUpperCase()) == 0);
	}

	public static String readLine(BufferedInputStream bis) throws IOException
	{
		StringBuffer line = new StringBuffer();
		int ch;
		while (true)
		{
			ch = bis.read();
			if (ch == -1)
				break;
			if (ch == '\r' || ch == '\n')
			{
				if (ch == '\r')
				{
					ch = bis.read();
				}
				break;
			}
			line.append((char) ch);
		}
		if (ch == -1 && line.length() == 0)
			return null;
		else
			return line.toString();
	}

	public static int read(BufferedInputStream bis) throws IOException
	{
		return bis.read();
	}

	public static byte[] addByteCapacity(byte[] rece)
	{
		byte[] temp = new byte[rece.length + 1024];
		System.arraycopy(rece, 0, temp, 0, rece.length);
		return temp;
	}
	
	public static void sendInitHeader(BufferedOutputStream client_out) throws IOException
	{
		sendHeaderDate(client_out);
		sendHeaderContentType(client_out);
		sendHeaderConnection(client_out);
		sendHeaderAcceptRanges(client_out);
		sendHeaderServer(client_out);
	}
	
	public static void sendHeaderDate(BufferedOutputStream client_out) throws IOException
	{
		client_out.write(("Date: " + HttpUtil.dataFormatGMT.format(new Date()) + HttpUtil.CRLF).getBytes());
	}
	
	public static void sendHeaderContentType(BufferedOutputStream client_out) throws IOException
	{
		client_out.write(("Content-Type: text/html"+ HttpUtil.CRLF).getBytes());
	}
	
	public static void sendHeaderConnection(BufferedOutputStream client_out) throws IOException
	{
		client_out.write(("Connection: close"+ HttpUtil.CRLF).getBytes());
	}
	
	public static void sendHeaderAcceptRanges(BufferedOutputStream client_out) throws IOException
	{
		client_out.write(("Accept-Ranges: bytes"+ HttpUtil.CRLF).getBytes());
	}
	
	public static void sendHeaderServer(BufferedOutputStream client_out) throws IOException
	{
		client_out.write(("Server: Microsoft-IIS/7.0"+ HttpUtil.CRLF).getBytes());
	}
	public static ThreadGroup getThreadGroup() {
		return threadGroup;
	}
}
